package com.qfedu.lc.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qfedu.lc.common.vo.R;
import com.qfedu.lc.user.entity.MoneyLog;
import com.qfedu.lc.user.entity.PayLog;
import com.qfedu.lc.user.entity.Wallet;
import com.qfedu.lc.user.mapper.MoneyLogMapper;
import com.qfedu.lc.user.mapper.PayLogMapper;
import com.qfedu.lc.user.mapper.WalletMapper;
import com.qfedu.lc.user.service.PayLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 10.支付记录表 服务实现类
 * </p>
 *
 * @author wgz
 * @since 2024-06-12
 */
@Service
public class PayLogServiceImpl extends ServiceImpl<PayLogMapper, PayLog> implements PayLogService {

    @Autowired
    private PayLogMapper payLogMapper;

    @Autowired
    private WalletMapper walletMapper;

    @Autowired
    private MoneyLogMapper moneyLogMapper;

    @Override
    public R alipayCallBack(String no) {

        // 1.修改支付订单状态
        QueryWrapper<PayLog> payLogQueryWrapper = new QueryWrapper<>();
        payLogQueryWrapper.eq("no",no);
        PayLog payLog = payLogMapper.selectOne(payLogQueryWrapper);

        if (payLog==null){
            return R.fail("支付失败");
        }

        if (no.startsWith("z")){ // 支付宝充值账户余额
            //  2. 钱包增加余额
            QueryWrapper<Wallet> walletQueryWrapper = new QueryWrapper<>();

            walletQueryWrapper.eq("user_id",payLog.getUserId());
            Wallet wallet = walletMapper.selectOne(walletQueryWrapper);

            if (wallet==null){ // 可以创建钱包 插入即可

                wallet = new Wallet();
                wallet.setUserId(payLog.getUserId());
                wallet.setMoney(payLog.getMoney()/100);
                wallet.setTotalMoney(payLog.getMoney()/100);
                wallet.setCtime(new Date());
                wallet.setUtime( new Date());

                int num = walletMapper.insert(wallet);
                if (num<=0){
                    return R.fail("支付失败");
                }


            }else {  // 跟新钱包
                wallet.setMoney(wallet.getMoney() + payLog.getMoney()/100);
                wallet.setTotalMoney(wallet.getTotalMoney() + payLog.getMoney()/100);

                int num = walletMapper.updateById(wallet);
                if (num<=0){
                    return R.fail("支付失败");
                }

            }


            //  3. 写入钱包增加日志

            MoneyLog moneyLog = new MoneyLog();

            moneyLog.setUserId(payLog.getUserId());
            moneyLog.setMoney(payLog.getMoney()/100);
            moneyLog.setType(1);//减少
            moneyLog.setInfo("爱贝充值");
            moneyLog.setFlag("充值");
            moneyLog.setCtime(new Date());


            moneyLogMapper.insert(moneyLog);

            return R.ok("充值成功");

        }else if (no.startsWith("v")){ // vip 使用支付宝购买

        }



        return R.fail("充值失败");

    }
}
